﻿#include <cstdio>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>

#define BUFFLEN 1024
#define SERVER_PORT 8888

//udp循环服务器
void StartServer() {
    struct sockaddr_in local, to;
    time_t now;
    char buff[BUFFLEN];
    socklen_t len = sizeof(to);
    int s = socket(AF_INET, SOCK_DGRAM, 0);
    memset(&local, 0, sizeof(local));

    local.sin_family = AF_INET;
    local.sin_addr.s_addr = htonl(INADDR_ANY);
    local.sin_port = htons(SERVER_PORT);
    bind(s, (sockaddr*)&local, sizeof(local));
    //处理过程
    while (true)
    {
        memset(buff, 0, BUFFLEN);
        ssize_t n = recvfrom(s, buff, BUFFLEN, 0, (sockaddr*)&to,&len);
        if (n > 0 && !strncmp(buff, "TIME", 4))
        {
            memset(buff, 0, BUFFLEN);
            now = time(NULL);
            sprintf(buff, "%24s\r\n", ctime(&now));
            sendto(s, buff, strlen(buff), 0, (sockaddr*)&to, len);
            //发送数据
        }
    }
    close(s);
}
//udp客户端
void StartUdpClient() {
    struct sockaddr_in client;
    char buff[BUFFLEN];
    socklen_t len = 0;
    int s = socket(AF_INET, SOCK_DGRAM, 0);
    memset(&client, 0, sizeof(client));
    client.sin_family = AF_INET;
    client.sin_addr.s_addr = htonl(INADDR_ANY);
    client.sin_port = htons(SERVER_PORT);
    memset(buff, 0, BUFFLEN);
    strcpy(buff, "TIME");

    sendto(s, buff, strlen(buff), 0, (sockaddr*)&client, sizeof(client));

    memset(buff, 0, BUFFLEN);
    socklen_t clen = sizeof(client);

    int n = recvfrom(s, buff, BUFFLEN, 0, (sockaddr*)&client, &clen);
    if (n > 0)
    {
        printf("TIME:%s", buff);
    }
    close(s);

}

void StartTcpServer() {
    int s_s, s_c;
    struct sockaddr_in local, from;
    time_t now;
    char buff[BUFFLEN];
    int n = 0;
    s_s = socket(AF_INET, SOCK_STREAM, 0);
    socklen_t len = sizeof(from);

    memset(&local, 0, sizeof(local));
    local.sin_family = AF_INET;
    local.sin_addr.s_addr = htonl(INADDR_ANY);
    local.sin_port = htons(SERVER_PORT);

    bind(s_s, (sockaddr*)&local, sizeof(local));
    listen(s_s, 5);

    //处理过程
    while (true)
    {
        s_c = accept(s_s, (sockaddr*)&from, &len);
        memset(buff, 0, BUFFLEN);
        n = recv(s_c, buff, BUFFLEN, 0);
        if (n > 0 && !strncmp(buff, "TIME", 4))
        {
            memset(buff, 0, BUFFLEN);
            now = time(NULL);
            sprintf(buff, "%24s\r\n", ctime(&now));
            send(s_c, buff, strlen(buff), 0);
        }
        close(s_c);
    }
    close(s_s);
}
void startTcpClient() {
    int s;
    struct sockaddr_in tClient;
    char buff[BUFFLEN];
    int n = 0;

    s = socket(AF_INET, SOCK_STREAM, 0);
    memset(&tClient, 0, sizeof(tClient));
    tClient.sin_family = AF_INET;
    tClient.sin_addr.s_addr = htonl(INADDR_ANY);
    tClient.sin_port = htons(SERVER_PORT);

    connect(s, (sockaddr*)&tClient, sizeof(tClient));
    memset(buff, 0, BUFFLEN);

    strcpy(buff, "TIME");

    send(s, buff, strlen(buff), 0);
    memset(buff, 0, BUFFLEN);
    n = recv(s, buff, BUFFLEN, 0);
    if (n > 0)
    {
        printf("TIME:%s", buff);
    }
    close(s);

}
void test() {
    pid_t pid = fork();
    if (pid == 0)
    {
        StartUdpClient();
        return;
    }
    else if (pid > 0)
    {
        StartServer();
        return;
    }
}

int main()
{
    test();
    printf("%s 向你问好!\n", "NetWhile");
    return 0;
}